﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Evoucher.Business.Models;

public partial class admin_vouchers_PackageItems : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadVoucherTypes();
            LoadPackageItems();
        }
    }

    private void LoadVoucherTypes()
    {
        if (Request.QueryString["id"] == null)
            return;

        IVoucherRepository repo = new VoucherRepository();
        List<VoucherType> voucherTypes = repo.GetVoucherTypes(); 

        ddlVoucherType1.DataSource = voucherTypes;
        ddlVoucherType2.DataSource = voucherTypes;
        ddlVoucherType3.DataSource = voucherTypes;

        ddlVoucherType1.DataBind();
        ddlVoucherType2.DataBind();
        ddlVoucherType3.DataBind();
    }

    private void LoadPackageItems()
    {
        if (Request.QueryString["id"] == null)
            return;

        int id = Convert.ToInt32(Request.QueryString["id"]);
        IVoucherRepository repo = new VoucherRepository();
        List<PackageItem> items = repo.GetPackageItemsByPackageId(id);

        int i = 0;
        foreach (PackageItem item in items) {
            i++;

            item.VoucherTypeReference.Load();

            switch (i)
            {
                case 1:
                    txtCount1.Text = item.VoucherCount.ToString();
                    ddlVoucherType1.Items.FindByValue(item.VoucherType.Id.ToString()).Selected = true;
                    break;
                case 2:
                    txtCount2.Text = item.VoucherCount.ToString();
                    ddlVoucherType2.Items.FindByValue(item.VoucherType.Id.ToString()).Selected = true;
                    break;
                case 3:
                    txtCount3.Text = item.VoucherCount.ToString();
                    ddlVoucherType3.Items.FindByValue(item.VoucherType.Id.ToString()).Selected = true;
                    break;
                default:
                    break;
            }
        }
    }

    private void NewPackageItems()
    {
        if (Request.QueryString["id"] == null)
            return;

        int id = Convert.ToInt32(Request.QueryString["id"]);
        IVoucherRepository repo = new VoucherRepository();
        Package p = repo.GetPackageById(id);

        if (!String.IsNullOrEmpty(txtCount1.Text) && ddlVoucherType1.SelectedValue != "-1")
        {
            PackageItem pi = new PackageItem();
            pi.VoucherCount = Convert.ToInt32(txtCount1.Text);
            pi.VoucherType = repo.GetVoucherTypeById( Convert.ToInt32(ddlVoucherType1.SelectedValue) );
            pi.Package = p;
            repo.AddItemsToPackage(pi);
        }

        if (!String.IsNullOrEmpty(txtCount2.Text) && ddlVoucherType2.SelectedValue != "-1")
        {
            PackageItem pi = new PackageItem();
            pi.VoucherCount = Convert.ToInt32(txtCount2.Text);
            pi.VoucherType = repo.GetVoucherTypeById(Convert.ToInt32(ddlVoucherType2.SelectedValue));
            pi.Package = p;
            repo.AddItemsToPackage(pi);
        }

        if (!String.IsNullOrEmpty(txtCount3.Text) && ddlVoucherType3.SelectedValue != "-1")
        {
            PackageItem pi = new PackageItem();
            pi.VoucherCount = Convert.ToInt32(txtCount3.Text);
            pi.VoucherType = repo.GetVoucherTypeById(Convert.ToInt32(ddlVoucherType3.SelectedValue));
            pi.Package = p;
            repo.AddItemsToPackage(pi);
        }

        repo.persist();
    }

    private void SavePackageChanges()
    {
        if (Request.QueryString["id"] == null)
            return;

        int id = Convert.ToInt32(Request.QueryString["id"]);
        IVoucherRepository repo = new VoucherRepository();
        List<PackageItem> items = repo.GetPackageItemsByPackageId(id);

        int i = 0;
        foreach (PackageItem item in items)
        {
            i++;

            switch (i)
            {
                case 1:
                    item.VoucherCount = Convert.ToInt32(txtCount1.Text);
                    item.VoucherType = repo.GetVoucherTypeById(Convert.ToInt32(ddlVoucherType1.SelectedValue));
                    break;
                case 2:
                    item.VoucherCount = Convert.ToInt32(txtCount2.Text);
                    item.VoucherType = repo.GetVoucherTypeById(Convert.ToInt32(ddlVoucherType2.SelectedValue));
                    break;
                case 3:
                    item.VoucherCount = Convert.ToInt32(txtCount3.Text);
                    item.VoucherType = repo.GetVoucherTypeById(Convert.ToInt32(ddlVoucherType3.SelectedValue));
                    break;
                default:
                    break;
            }
        }

        repo.persist();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] == null)
            return;

        int id = Convert.ToInt32(Request.QueryString["id"]);
        IVoucherRepository repo = new VoucherRepository();
        Package p = repo.GetPackageById(id);
        p.PackageItems.Load();

        if (p.PackageItems.Count == 0)
            NewPackageItems();
        else
            SavePackageChanges();

        Mani.Common.UIHelper.ClientAlert(this, "تغییرات با موفقیت ذخیره شد");
        Mani.Common.UIHelper.ClientRedirect(this, "packages.aspx");
    }
}